// ------------------------------------
// Reducer
// ------------------------------------
import { createReducer, statusToError, getStatusError } from 'http-services'
import * as types from './constant'
import * as storageService from 'services/storage'
import { setToken } from 'services/token'

const initState = {
  getListPending: false,
  getListError: -1,
  getListMessage: null,
  listData: null,
  items: null,
  paths: null,
  srv: null,
  rooms: null,
  roomfish: null,
  MODIFY_ROLE_TYPE: null,
  DAILY_REPORT_PUSH: null,
  ADVICE_MAIL: null,
  parser: null,
  postDataPending: false,
  postDataError: -1,
  postDataMessage: null,
  postDataProto: null,
  postData: null,
  postDataTest: null
}

const ACTION_HANDLERS = {
  [types.PROTO_FETCH_GETLIST_BEGIN]: (state, action) => {
    return {
      ...state,
      getListPending: true,
      getListError: -1,
      getListMessage: null
    }
  },
  [types.PROTO_FETCH_GETLIST_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { 
        listData: data.protoAPI,
        ..._.pick(data, ['MODIFY_ROLE_TYPE', 'DAILY_REPORT_PUSH', 'ADVICE_MAIL', 'items', 'paths', 'srv', 'rooms', 'roomfish', 'parser'])
      }
    }
    return {
      ...state,
      ...statusToError(payload, 'getListError', 'getListMessage'),
      getListPending: false,
      ...newState
    }
  },
  [types.PROTO_FETCH_GETLIST_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'getListError', 'getListMessage'),
      getListPending: false,
    }
  },
  [types.PROTO_FETCH_POSTDATA_BEGIN]: (state, action) => {
    const { payload } = action
    let newState = getNewState(state, payload.proto, payload.options, null)
    return {
      ...state,
      postDataPending: true,
      postDataError: -1,
      postDataMessage: null,
      postDataProto: payload.proto,
      ...newState
    }
  },
  [types.PROTO_FETCH_POSTDATA_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      let options = /(\?)(\S+)$/.test(state.postDataProto) ? state.postDataTest.opts : state.postData[state.postDataProto].opts
      newState = getNewState(state, state.postDataProto, options, data)
    }
    return {
      ...state,
      ...statusToError(payload, 'postDataError', 'postDataMessage'),
      postDataPending: false,
      ...newState
    }
  },
  [types.PROTO_FETCH_POSTDATA_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'postDataError', 'postDataMessage'),
      postDataPending: false,
    }
  },
  [types.PROTO_FETCH_POSTDATA_STOP]: (state, action) => {
    return {
      ...state,
      postDataPending: false,
      postDataError: 1,
      postDataMessage: null,
    }
  }
}

export default (state = initState, action) => createReducer(state, action, ACTION_HANDLERS)

const getNewState = (state, proto, options = null, data = null) => {
  let newState = null
  if (/(\?)(\S+)$/.test(proto)) {
    newState = {
      postDataTest: { 
        ...state.postDataTest,
        data: data
      }
    }
    if (options) {
      newState.postDataTest.opts = {
        ...options, 
        proto: proto.replace(/(\?)(\S+)$/, '')
      }
    }
  }
  else {
    let stateData = _.has(state.postData, proto) ? state.postData[proto].data : data
    newState = {
      postData: {
        ...state.postData,
        [proto]: { data: data ? (data.msgbody.data || data.msgbody) : stateData }
      }
    }
    if (options) {
      newState.postData[proto].opts = options
    }
  }
  return newState
}